package com.example.sixtrainingdemo.presenter;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.example.sixtrainingdemo.contract.AddRoundContract;
import com.example.sixtrainingdemo.entity.Photo;
import com.example.sixtrainingdemo.entity.Round;
import com.example.sixtrainingdemo.entity.RoundPhoto;

import java.util.List;

public class AddRoundPresenter implements AddRoundContract.IAddRoundPresenter {

   private AddRoundContract.IAddRoundView mAddRoundView;
   private Context mContext;

   public AddRoundPresenter(AddRoundContract.IAddRoundView addRoundView, Context context) {
      mAddRoundView = addRoundView;
      mContext = context;
   }

   @Override
   public void saveRound(Round round) {
      new Thread(new Runnable() {
         @Override
         public void run() {
            //商品插入
            ContentValues cv = new ContentValues();
            cv.put("round_message", round.getMessage());
            Uri roundUrl = Uri.parse("content://com.example.sixtrainingdemo.provider/round");
            Uri insertUri = mContext.getContentResolver().insert(roundUrl, cv);
            long newRowId = ContentUris.parseId(insertUri);
            Log.d("", "新插入的商品id ===== " + newRowId);
            List<RoundPhoto> roundphotoList = round.getRoundPhotoList();
            Log.d("", "roundphotoList ===== " + roundphotoList.size());
            ContentValues[] arrayValues = new ContentValues[roundphotoList.size()];
            for (int i = 0; i < roundphotoList.size() - 1; i++) {
               ContentValues contentValues = new ContentValues();
               contentValues.put("round_id", newRowId);
               contentValues.put("image", roundphotoList.get(i).getData());
               arrayValues[i] = contentValues;
               Log.d("", "arrayValues ===== " + i);
            }
            Uri roundphotoUrl = Uri.parse("content://com.example.sixtrainingdemo.provider/roundphoto");
            //批量插入图片
            int count = mContext.getContentResolver().bulkInsert(roundphotoUrl,arrayValues);
            Log.d("", "count ===== " + count);
         }
      }).start();
   }
}
